package org.wutopia.labcloud.domain.uaa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.UserProductCartAddInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.UserProductCartDeleteInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.output.UserCartGetListOutputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.output.UserProductGetLIstOutput;
import org.wutopia.labcloud.domain.uaa.dao.USerProductCartDao;
import org.wutopia.labcloud.domain.uaa.dao.entity.UserProductCart;
import org.wutopia.labcloud.domain.uaa.service.UserProductCartService;
import org.wutopia.labcloud.service.api.service.dubbo.DubboProductService;
import org.wutopia.labcloud.service.api.service.dubbo.dto.output.DubboGetProductOutputDTO;

import java.util.ArrayList;
import java.util.List;


@Service
@AllArgsConstructor
@Slf4j
public class UserProductCartServiceImpl implements UserProductCartService {

    private USerProductCartDao userProductCartDao;

    @DubboReference
    private DubboProductService dubboProductService;

    @Override
    public UserCartGetListOutputVO getList(Long userId) {

        QueryWrapper<UserProductCart> queryWrapper  = new QueryWrapper<>();

        queryWrapper.eq("user_id", userId);

        List<UserProductCart> userProductCarts = userProductCartDao.selectList(queryWrapper);

        List<UserProductGetLIstOutput> list = new ArrayList<>();

        for (UserProductCart cartItem: userProductCarts) {
            DubboGetProductOutputDTO productById = dubboProductService.getProductById(cartItem.getProduct_id());

            UserProductGetLIstOutput output = new UserProductGetLIstOutput();

            output.setId(cartItem.getId());
            output.setUserId(cartItem.getUser_id());
            output.setProductId(cartItem.getProduct_id());

            output.setDescription(productById.getDescription());
            output.setRate(productById.getRate());
            output.setPrice(productById.getPrice());
            output.setTitle(productById.getTitle());
            output.setCoverAddr(productById.getCoverAddr());
            list.add(output);
        }

        UserCartGetListOutputVO vo = new UserCartGetListOutputVO();
        vo.setList(list);

        return vo;
    }

    @Override
    public void add(UserProductCartAddInputVO vo) {

        UserProductCart cart = new UserProductCart();

        cart.setProduct_id(vo.getProductId());
        cart.setUser_id(vo.getUserId());

        userProductCartDao.insert(cart);
    }

    @Override
    public void delete(UserProductCartDeleteInputVO vo) {

        List<Long> cartItemIdList = vo.getCartItemIdList();

        for(Long cartItemId : cartItemIdList) {
            userProductCartDao.deleteById(cartItemId);
        }
    }
}
